package com.ruitu.ssmic.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.ssmic.R;
import com.ruitu.ssmic.view.CountDownButton;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class VersionPasswordFragment extends BaseFragment {
    private static String phone;
    private TextView tv_phone;
    private CountDownButton count_down_btn;

    public static VersionPasswordFragment newInstance() {
        Bundle args = new Bundle();
        VersionPasswordFragment fragment = new VersionPasswordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_version_password, null);
        tv_phone = view.findViewById(R.id.tv_phone);
        count_down_btn = view.findViewById(R.id.count_down_btn);
        count_down_btn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!Arad.bus.isRegistered(this)) {
            Arad.bus.register(this);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (count_down_btn == v) {
            count_down_btn.start();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setPhone(EventModel eventModel) {
        if (eventModel.getEventCode() == 19040300) {
            tv_phone.setText((String) eventModel.getEventObj());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //页面销毁取消倒计时
        if (!count_down_btn.isFinish()) {
            count_down_btn.cancle();
        }
    }
}
